#! /usr/bin/env python
#
# SolarTango --- simulator.py
# Copyleft 2013 Galatolo Federico
#
# This software is a free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

import pygame, time, math, sys, threading

class Simulator():
	astro = []  #(dist,periodo),(dist,periodo)
	speed = 0
	duration = 0
	mode = 0
	inc = 0.001
	sleeptime = 0.01
	zoom = 0
	size = 800
	status = "go"
	screen = False
	points = []
	t = 0
	pianeta = ((0, 0, 255),5)  #colore RGB, dimensioni
	traccia = ((225,0,0),3)     # idem
	inc = 0.001
	sleeptime = 0.001

	
	def __init__(self,astro,speed,duration,mode):
		self.points = []
		self.astro = astro
		self.speed = speed
		self.duration = duration
		self.mode = mode
		self.zoom = ((self.size/2)-50)/astro[0][0]
	
	def rebuild_points(self):
		for point in self.points:
			pygame.draw.ellipse(self.screen,self.traccia[0],[point[0],point[1],self.traccia[1],self.traccia[1]],0)
	
	def linea(self,a,b,medio=True):
		xm = (a[0]+b[0])/2
		ym = (a[1]+b[1])/2
		pygame.draw.line(self.screen,(255,255,255),(a[0],a[1]),(b[0],b[1]))
		if medio == True:
			pygame.draw.circle(self.screen,self.traccia[0],(xm,ym),self.traccia[1])
		return (xm,ym)
		
	def createwin(self):
		pygame.init()
		self.screen = pygame.display.set_mode((self.size,self.size))
		pygame.display.set_caption("SolarTango")
		
	def destroywin(self):
		pygame.display.quit()
		
	def save(self,imgname):
		pygame.image.save(self.screen,imgname)
		
	def buildtext(self):
		if self.mode == 1:
			text = " - Punto medio tra "+self.astro[0][2]+" e "+self.astro[1][2]
		if self.mode == 2:
			text = " - Punto medio dei punti medi tra "+self.astro[0][2]+", "+self.astro[1][2]+" e "+self.astro[2][2]
		if self.mode == 3:
			text = " - Baricentro tra "+self.astro[0][2]+", "+self.astro[1][2]+" e "+self.astro[2][2]
		return text
		
	def next(self):
		if int(self.t*self.speed/self.astro[0][1]) != self.duration or self.duration == 0:
			self.t = self.t + self.inc
			xy = []
			self.screen.fill((0,0,0))
					
			pygame.draw.circle(self.screen,(255,201,1),[self.size/2,self.size/2],25) #il sole!
			for p in self.astro:
				xy.append((int((self.size/2)+math.sin(self.speed*((2*math.pi)/p[1])*self.t)*p[0]*self.zoom),int((self.size/2)+math.cos(self.speed*((2*math.pi)/p[1])*self.t)*p[0]*self.zoom)))
			for draw in xy:
				pygame.draw.circle(self.screen,self.pianeta[0],[draw[0],draw[1]],self.pianeta[1])
			
			if int(self.t*self.speed)/self.astro[0][1] > 0:
				text = str(int(self.t*self.speed)/self.astro[0][1])+" Anni e "+str(int(self.t*self.speed%self.astro[0][1]))+" Giorni"
			else:
				text = str(int(self.t*self.speed))+" Giorni"
			text = text + self.buildtext()
			font = pygame.font.SysFont("ubuntu", 15)
			label = font.render(text, 1, (255,255,255))
			self.screen.blit(label, (0, self.size-20))

			
			if self.mode == 1:	
				(xm,ym) = self.linea((xy[0][0],xy[0][1]),(xy[1][0],xy[1][1]))
				self.points.append((xm,ym))
			
			if self.mode == 2:
				(xm1,ym1) = self.linea((xy[0][0],xy[0][1]),(xy[1][0],xy[1][1]))
				(xm2,ym2) = self.linea((xy[1][0],xy[1][1]),(xy[2][0],xy[2][1]))
				(xm,ym) = self.linea((xm1,ym1),(xm2,ym2))
				self.points.append((xm,ym))
				
			if self.mode == 3:
				self.linea((xy[0][0],xy[0][1]),(xy[1][0],xy[1][1]),False)
				self.linea((xy[1][0],xy[1][1]),(xy[2][0],xy[2][1]),False)
				self.linea((xy[2][0],xy[2][1]),(xy[0][0],xy[0][1]),False)
				xm = (xy[0][0]+xy[1][0]+xy[2][0])/3
				ym = (xy[0][1]+xy[1][1]+xy[2][1])/3
				pygame.draw.circle(self.screen,self.traccia[0],(xm,ym),self.traccia[1])
				self.points.append((xm,ym))
			
			if self.mode == 4:
				(xm1,ym1) = self.linea((xy[0][0],xy[0][1]),(xy[1][0],xy[1][1]))
				(xm2,ym2) = self.linea((xy[2][0],xy[2][1]),(xy[3][0],xy[3][1]))
				(xm,ym) = self.linea((xm1,ym1),(xm2,ym2))
				self.points.append((xm,ym))
				
				
			self.rebuild_points()
			pygame.display.flip()
			
			
				

